import { Store } from 'vuex';
import createPersistedState from 'vuex-persistedstate';

const persistedState = createPersistedState({
    'storage': window.localStorage
});

interface IState {
    userInfo:{
        secret:string
        id:string
        [key:string]:any
    }
    tagData:tagObj[]
    code:string
}
const defaultState:IState = {
    'userInfo': {
        'secret': '',
        'id': '',
        'token':''
    },
    'code':'',
    'tagData':[]
};

export default new Store({
    'plugins': [persistedState],
    'state': defaultState,
    'mutations': {
        _userInfo(state, data = {}) {
            const oldData = JSON.parse(JSON.stringify(data));
            delete oldData.auths;
            state.userInfo = oldData || {};
        },
        _logout(state, opt) {
            state.userInfo = {
                'secret': '',
                'id': ''
            };
            state.code = '';
        },
        _modifyInfo(state, data = {}){
            state.userInfo = data;
        },
        _setTagData(state, data) {
            state.tagData = data;
        },
        _setCode(state, data) {
            state.code = data;
        }
    },
    'actions': {
        '-userInfo': ({ commit, state }, opt) => {
            commit('_userInfo', opt);
        },
        '-logout': ({ commit, state }, opt) => {
            commit('_logout', opt);
        },
        '-_setTagData': ({ commit, state }, opt) => {
            commit('_setTagData', opt);
        }
    },
    'modules': {
    }
});
